package com.qqtech.qquan.vote.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.enums.CircleTopicBizzTypeDictEnum;
import com.qqtech.qquan.circle.enums.CircleTopicStatusDictEnum;
import com.qqtech.qquan.circle.model.CircleTopicQuery;
import com.qqtech.qquan.circle.model.CircleTopicVo;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.hi.service.HiCircleService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.vote.model.VoteItemQuery;
import com.qqtech.qquan.vote.model.VoteItemVo;
import com.qqtech.qquan.vote.model.VoteResult;
import com.qqtech.qquan.vote.model.VoteResultQuery;
import com.qqtech.qquan.vote.model.VoteResultVo;
import com.qqtech.qquan.vote.service.VoteBaseService;
import com.qqtech.qquan.vote.service.VoteItemService;
import com.qqtech.qquan.vote.service.VoteResultService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/vote/result")
public class VoteResultController extends BaseWebController<VoteResult, VoteResultQuery> {
	@Resource
	private VoteResultService voteResultService;
	
	@Resource
	private CircleTopicService circleTopicService;
	
	@Resource
	private UserBaseService userBaseService;
	
	@Resource
	private VoteBaseService voteBaseService;

	@Resource
	private VoteItemService voteItemService;
	
	@Resource
	private HiCircleService hiCircleService;

	@Override
	protected BaseService<VoteResult> getBaseService() {
		return voteResultService;
	}

	/**
	 * 新增
	 * 
	 * @param voteBaseVo	
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(@RequestBody VoteResultVo voteResultVo) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		boolean isResult = voteResultService.isResult(voteResultVo.getVoteId(), curUserId);
		if (isResult) {
			result.addErr("您已经投过票了。");
			return result;
		}
		CircleTopicQuery query = new CircleTopicQuery();
		query.setBizzType(CircleTopicBizzTypeDictEnum.VOTE.getCode());
		query.setBizzId(voteResultVo.getVoteId());
		query.setStatus(CircleTopicStatusDictEnum.NORMAL.getCode());
		CircleTopicVo circleTopicVo=circleTopicService.getOne(query, new PageOrder());
		if (null==circleTopicVo) {
			result.addErr("该投票已经删除了。");
			return result;
		}
		if (null != voteResultVo.getVoteItemId()) {// 单选
			voteResultVo.setUserId(curUserId);
			voteResultService.insert(voteResultVo);
			//荣誉系统-ryz
			hiCircleService.addVoteResult(curUserId, circleTopicVo.getId());
		} else if (StringUtil.isNotBlank(voteResultVo.getVoteItemIds())) {// 多选
			Iterator<String> voteItemIds = Splitter.on(";").trimResults().omitEmptyStrings()
					.split(voteResultVo.getVoteItemIds()).iterator();
			List<String> voteItemIdList = Lists.newArrayList(voteItemIds);
			List<VoteResult> voteResultList = new ArrayList<VoteResult>();
			for (String voteItemId : voteItemIdList) {
				VoteResult voteResult = new VoteResult();
				voteResult.setVoteId(voteResultVo.getVoteId());
				voteResult.setUserId(curUserId);
				voteResult.setVoteItemId(Integer.valueOf(voteItemId));
				voteResultList.add(voteResult);
			}
			if (voteResultList.size() > 0) {
				voteResultService.insertInBatch(voteResultList);
				//荣誉系统-ryz
				hiCircleService.addVoteResult(curUserId, circleTopicVo.getId());
			}
		}

		VoteItemQuery voteItemQuery = new VoteItemQuery();
		voteItemQuery.setVoteId(voteResultVo.getVoteId());
		List<VoteItemVo> list = voteItemService.queryList(voteItemQuery);
		Integer voteItemResult;
		for (VoteItemVo voteItemVo : list) {
			voteItemResult = voteResultService.getVoteItemResult(voteItemQuery.getVoteId(), voteItemVo.getId());
			voteItemVo.setPercent(voteItemResult);
		}
		result.put("list", list);
		return result;
	}
	
	/**
	 * 投票成员
	 * 
	 * @param VoteResultQuery
	 * @return
	 */
	@RequestMapping(value = "/memberVote")
	@ResponseBody
	public Result memberVote(@RequestBody VoteResultQuery voteResultQuery) {
		Result result = new Result();
		Pageable pageKit = new PageKit(voteResultQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<VoteResultVo> page = voteResultService.queryPageList(voteResultQuery, pageKit);
		List<VoteResultVo> voteResultList = page.getContent();
		UserBaseVo userBaseVo=null;
		for (VoteResultVo voteResultVo : voteResultList) {
			userBaseVo=userBaseService.getById(voteResultVo.getUserId());
			if(userBaseVo!=null){
				voteResultVo.setUserNick(userBaseVo.getNick());
				voteResultVo.setUserLogo(userBaseVo.getFullLogo());
			}
		}
		result.put("page", page);
		return result;
	}
}
